const { setUserList, queryUser, queryUserOfUser, queryUserOfCoverUser, queryContent, setUserContent } = require("../model/contactsModel")

module.exports = {
  getContactsList: async (ctx) => {
    const { user_id } = ctx.request.query;
    let res = await queryUser(user_id);
    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "无",
        data: []
      }
    }
  },
  sendOut: async (ctx) => {
    const { user_id, cover_user_id, user_name, user_avatar, cover_user_name, cover_user_avatar, content, createtime } = ctx.request.body;

    let q1 = await queryUserOfUser(user_id, cover_user_id);
    if (q1.length !== 0) {
      return ctx.body = {
        status: 205,
        msg: "已有"
      }
    }
    let q2 = await queryUserOfCoverUser(user_id, cover_user_id);
    if (q2.length !== 0) {
      return ctx.body = {
        status: 205,
        msg: "已有"
      }
    }
    let res = await setUserList(user_id, cover_user_id, user_name, user_avatar, cover_user_name, cover_user_avatar, content, createtime);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "请求成功",
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "请求失败",
      }
    }
  },
  getcontentlist: async (ctx) => {
    const { user_id, cover_user_id } = ctx.request.query;
    let res = await queryContent(user_id, cover_user_id);
    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "无",
        data: []
      }
    }
  },
  sendContent: async (ctx) => {
    const { user_id, cover_user_id, user_name, user_avatar, content, createtime } = ctx.request.body;
    let res = await setUserContent(user_id, cover_user_id, user_name, user_avatar, content, createtime);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "请求成功",
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "请求失败",
      }
    }
  }
}